package com.android.projectsquirrel;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class camera extends Activity {

    //YOU CAN EDIT THIS TO WHATEVER YOU WANT
    private static final int SELECT_PICTURE = 1;

	protected static final String ACTION_CAMERA_BUTTON = null;

	protected static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;

    private String selectedImagePath;
    //ADDED
    private String filemanagerstring;
    private String path;
    private Uri imageUri;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);

         (findViewById(R.id.picture))
        .setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                // in onCreate or any event where your want the user to
                // select a file
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), SELECT_PICTURE);

            }
        });
         (findViewById(R.id.camera)).setOnClickListener(new OnClickListener(){
        	 public void onClick(View arg0){
        		 String fileName = "new-photo-name.jpg";
        		 //create parameters for Intent with filename

        		 ContentValues values = new ContentValues();

        		 values.put(MediaStore.Images.Media.TITLE, fileName);
        		 
        		 values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
        		 
        		 //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)


        		 imageUri = getContentResolver().insert(
        		 
        		 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        		 
        		 //create new Intent
        		 
        		 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        		 
        		 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        		 
        		 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        		 
        		 startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

        	 }
         });
         Button squirrelButton = (Button) findViewById(R.id.home);
     	squirrelButton.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
             	Intent myIntent = new Intent(view.getContext(),Main.class);
                 startActivity(myIntent);
             }
     	});
    }

    //UPDATED
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    		    if (resultCode == RESULT_OK) {
    		        //use imageUri here to access the image
    		    	path = imageUri.toString();
    		 
    		    } else if (resultCode == RESULT_CANCELED) {
    		        Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
    		    } else {
    		        Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
    		}
    		}

    	
    	if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();

                //OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();

                //MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);

                //DEBUG PURPOSE - you can delete this if you want
                if(selectedImagePath!=null)
                    System.out.println(selectedImagePath);
                else System.out.println("selectedImagePath is null");
                if(filemanagerstring!=null)
                    System.out.println(filemanagerstring);
                else System.out.println("filemanagerstring is null");

                //NOW WE HAVE OUR WANTED STRING
                if(selectedImagePath!=null)
                    System.out.println(selectedImageUri+" is the right one for you!");
                else
                    System.out.println("filemanagerstring is the right one for you!");
                path = selectedImagePath.toString();
            }
            
        }
    }

    //UPDATED!
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        else return null;
    }
}